package com.woniuxy.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.service.GoodsService;
import com.woniuxy.service.GoodsShowService;
import com.work.framework.pojo.Goods;
import com.work.framework.pojo.GoodsAuctionDTO;
import com.work.framework.pojo.ResultDTO;
import com.work.framework.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fengyao
 * @version 1.0
 * @date 2019/9/17 0017 上午 10:02
 */
@RequestMapping("/show")
@RestController
@EnableCaching
@Api(value = "商品展示类",tags = "商品展示模块")
public class GoodsShowHandler {
    @Resource
    GoodsShowService goodsShowService;

    @Resource
    GoodsService goodsService;
    /**
     *
     * @return 返回商品首页的所有结果的集合
     * 1.热点商品List
     * 2.Map类型的各类别
     */
    @ApiOperation(value = "返回首页信息",notes = "包含热点商品，分类热点商品，各个状态下的热点商品")
    @GetMapping("/selectPageHome")
    public ResultDTO selectPageHome(){
        /**
         * 1.根据收藏查找热点商品
         * 2.根据收藏查找各分类商品信息，各状态商品信息
         */
        Map result=new HashMap<String,Object>();
        Map classifyGoodsMap = goodsShowService.selectHotGoodsByClassify();
        Map statusGoodsMap = goodsShowService.selectHotGoodsByStatus();
        result.put("classifyGoodsMap",classifyGoodsMap);
        result.put("statusGoodsMap",statusGoodsMap);
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setCode(200);
        resultDTO.setData(result);
        resultDTO.setMessage("成功");
        return resultDTO;
    }

    @GetMapping("/selectShowGoodsList/{countyid}/{cid}/{sortType}/{sort}/{name}/{pageNum}/{pageSize}")
    @ApiOperation(value = "商品列表展示",notes = "根据商品的地点，类别选择")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品所在城市id",required = false,name = "countyid",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品分类id",required = false,name = "cid",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "排序类型",required = false,name = "sortType",dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "正序倒序",required = false,name = "sort",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "当前页面",required = false,name = "pageNum",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "页面大小",required = false,name = "pageSize",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "页面大小",required = false,name = "name",dataType = "String",paramType = "query"),
    })
    public ResultDTO selectShowGoodsList(@PathVariable Integer countyid,@PathVariable Integer cid,@PathVariable String sortType,@PathVariable Integer sort,@PathVariable String name,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        //1.查看是否获得商品分类信息与商品分类信息
        System.out.println("进入方法");
        System.out.println(name);
        List<GoodsAuctionDTO> goodsAuctionDTOS = goodsShowService.selectGoodsListToPage(countyid, cid, sortType, sort,name, pageNum, pageSize);
        ResultDTO resultDTO=new ResultDTO();
        if(!goodsAuctionDTOS.isEmpty()){
            resultDTO.setCode(200);
            resultDTO.setData(goodsAuctionDTOS);
            resultDTO.setMessage("成功");
        }else{
            resultDTO.setCode(500);
            resultDTO.setData(goodsAuctionDTOS);
            resultDTO.setMessage("数据为null");
        }
        //2.根据已有的分类信息带入查询方法并执行查询方法
        //3.将查询返回结果使用PageHelper分页并封装到ResultDTO
        //4.将ResultDTO返回给前台
        return resultDTO;
    }


    /**
     *
     * @param gid 商品详情信息
     * @return 商品信息与商品图片信息
     */
    @GetMapping("/auditingGoods/{gid}")
    @ApiOperation(value = "获取待审核商品详情信息",notes="根据页面点击传入的商品gid")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品详情信息",name = "gid",dataType = "Integer",paramType = "query")
    })
    public ResultDTO selectAuditingGoods(@PathVariable Integer gid){
        //1.从前台获取gid信息，通过gid获取商品详情信息。
        GoodsAuctionDTO goodsAuctionDTO = goodsService.selectGoodsAndAuctionByGid(gid);
        //2.通过gid得到图片信息
        System.out.println(goodsAuctionDTO);

        List<String> otherPictures = goodsService.selectOtherPictures(gid);
        System.out.println(otherPictures);
        if(goodsAuctionDTO!=null) {
            goodsAuctionDTO.setOtherUrls(otherPictures);
            ResultDTO resultDTO = new ResultDTO(200,"成功",goodsAuctionDTO);
            return resultDTO;
        }else{
            ResultDTO resultDTO = new ResultDTO(500,"失败",null);
            return resultDTO;
        }
    }

    public ResultDTO selectGoodsByMoney(Integer min,Integer max){
        List<GoodsAuctionDTO> goodsAuctionDTOS = goodsShowService.selectGoodsByMoney(min, max);
        return new ResultDTO(200,"成功",goodsAuctionDTOS);
    }
}
